--#include "data\config\misc\ResourceRetrieveConfig.lua" once

function GmDoResRetrieve( sysarg, flag, param )
	print("GmDoResRetrieve, flag="..flag..", param="..param)
	if flag == 1 then
		if param == 0 then
			ClearRetrieveResourceTimes( sysarg )
			ShowUndoRetrieveTimes( sysarg, enDailyOpTimes_StoneTomb )
		else
		end
	elseif flag == 2 then
		local  opType = param
		ShowActorDailyOpTimes( sysarg, opType )
	elseif flag == 4 then
		MoveDoneAndRetrieveResTimes( sysarg ) 		--
	elseif flag == 5 then
		local newOpType = param
		ShowDailyDoneTimesBefore( sysarg, newOpType )
	elseif flag == 6 then
		SendResRetrieveData( sysarg )
	elseif flag == 11 then
		local idx = param
		RetrieveResourceOfUndo( sysarg, 1, idx ) 	--单次找回30%资源
	elseif flag == 12 then
		local idx = param
		RetrieveResourceOfUndo( sysarg, 1, idx ) 	--单次找回100%资源
	elseif flag == 13 then
		local days = param
		MoveDoneAndRetrieveResTimesDays( sysarg, days )
	end
end

--[[
登陆，不包括在线跨天
]]
function OnRetrieveResLogin( sysarg )
	InitRetrieveResource( sysarg )
	local logoutDays = Actor.getLastLogoutDays( sysarg )		--玩家已经离线N天
	--print("OnRetrieveResLogin, logoutDays="..logoutDays)
	if logoutDays <= 0 then
		return
	end
	MoveDoneAndRetrieveResTimesDays( sysarg, logoutDays )		--移动数据
end

--[[
在线跨天（不包括登陆）
]]
function ProcRetrieveResNewDay( sysarg )
	--print("ProcRetrieveResNewDay...")
	MoveDoneAndRetrieveResTimesDays( sysarg, 1 )			--依次移动当天、1天前、2天前的数据到1天前、2天前、3天前
end


--[[
依次移动当天、1天前、2天前的数据到1天前、2天前、3天前
]]
function MoveDoneAndRetrieveResTimes( sysarg )
	for idx, resCfg in ipairs( ResourceRetrieveCfg ) do
		local opType 	= resCfg.dailyOpType
		local opParam 	= resCfg.dailyOpParam
		RecordDailyDoneTimes( sysarg, opType, opParam )

		local newOpType = GetNewOpTypeByTypeAndParam( opType, opParam )
		RecordRetrieveResourceTimes( sysarg, newOpType )
	end
	SendResRetrieveData( sysarg )
end

--[[
连续移动N天的数据
]]
function MoveDoneAndRetrieveResTimesDays( sysarg, days )
	if days < 0 then
		return
	end

	if days <= 3 then 			--3天以内
		for i=1, days do
			MoveDoneAndRetrieveResTimes( sysarg )
		end
	else 		--清除前3天的操作记录和找回记录
		ClearDailyDoneTimesBefore( sysarg )
		ClearRetrieveResourceTimes( sysarg )
	end
	SendResRetrieveData( sysarg )
end


--[[
找回未完成次数的资源
]]
function RetrieveResourceOfUndo( sysarg, flag, idx )
	--print("RetrieveResourceOfUndo, flag="..flag..", idx="..idx)
	if flag < 1 or flag > 4 then
		return
	end

	if flag == 3 or flag == 4 then				--一键批量找回
		RetrieveResourceBatch( sysarg, flag )
		return
	end

	local retrieveCfg = ResourceRetrieveCfg[idx]
	if not retrieveCfg then
		return
	end

	if not Actor.checkActorLevel( sysarg, retrieveCfg.levelLimit[2], retrieveCfg.levelLimit[1] ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016,ttFlyTip)
      	return
  	end
	
	local opType 	= retrieveCfg.dailyOpType
	local opParam 	= retrieveCfg.dailyOpParam
	local awards 	= GetElemByOpenServerDay( retrieveCfg.resource )
	local undoTimes, getTimes, lastTimes = GetTimesOfResRetrieve( sysarg, opType, opParam, retrieveCfg.maxRetrieveTimes )
	--print("RetrieveResourceOfUndo, 3 days undoTimes="..undoTimes..", getTimes="..getTimes..", lastTimes="..lastTimes)
	if lastTimes <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.RetrieveRes001, ttFlyTip)
		return
	end

	local daysBefore = GetRetrieveResDay( sysarg, opType, opParam )
	--print("RetrieveResourceOfUndo, daysBefore="..daysBefore)
	if daysBefore <= 0 or daysBefore > 3 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.RetrieveRes001, ttFlyTip)
		return
	end

	local moneyType = nil
	local getRate = 0
	local moneyNum  = 0
	if flag == 1 then 									--单次找回30%(使用金币)
		moneyType = mtBindCoin
		getRate = 0.3
		moneyNum = retrieveCfg.feeDiscCoin
	elseif flag == 2 then 								--单次找回100%(使用绑元)
		moneyType = mtBindYuanbao
		getRate = 1
		moneyNum = retrieveCfg.feeAllBindYB
	else
		return
	end

	if not moneyType or getRate <= 0 or moneyNum <= 0 then
		return
	end

	if not CheckConsumeMoney( sysarg, moneyType, moneyNum, 1, false ) then
		return
	end

	if not DoConsumeMoney( sysarg, moneyType, moneyNum, rate, GameLog.clResRetrieveFee, Lang.ScriptTips.RetrieveLog01, false  ) then
		return
	end

	local resElem   = GetElemByOpenServerDay( retrieveCfg.resource )
	local resAwards = GetCommonAwardsWithRate( resElem.awards, getRate )
	if not resAwards then
		return
	end

	local newOpType = GetNewOpTypeByTypeAndParam( opType, opParam )
	if not AddRetrieveResTimes( sysarg, daysBefore, newOpType, 1 ) then		--领取1次
		return
	end

	GiveCommonAward(sysarg, resAwards, GameLog.clResRetrieveAward, Lang.ScriptTips.RetrieveLog02 ) 	--获得
	SendResRetrieveData( sysarg )
end

--[[
批量找回资源
]]
function RetrieveResourceBatch( sysarg, flag )
	--print("RetrieveResourceBatch, flag="..flag)
	if flag < 3 or flag > 4 then
		return
	end

	local moneyType = nil
	local getRate = 0
	if flag == 3 then 									--一键找回30%(使用金币)
		moneyType = mtBindCoin
		getRate = 0.3
	else
		moneyType = mtBindYuanbao
		getRate = 1
	end

	local moneyNum = 0
	for idx, retrieveCfg in ipairs( ResourceRetrieveCfg ) do
		if Actor.checkActorLevel( sysarg, retrieveCfg.levelLimit[2], retrieveCfg.levelLimit[1] ) then 		--等级要求
			local opType 	= retrieveCfg.dailyOpType
			local opParam 	= retrieveCfg.dailyOpParam
			local undoTimes, getTimes, lastTimes = GetTimesOfResRetrieve( sysarg, opType, opParam, retrieveCfg.maxRetrieveTimes )
			--print("RetrieveResourceOfUndo, idx="..idx..", opType="..opType..", lastTimes="..lastTimes)
			if lastTimes > 0 then				--lastTimes是扣钱的倍数
				if flag == 3 then
					moneyNum = moneyNum + retrieveCfg.feeDiscCoin * lastTimes
				else
					moneyNum = moneyNum + retrieveCfg.feeAllBindYB * lastTimes
				end
			end
		end
	end

	if not moneyType or getRate <= 0 or moneyNum <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.RetrieveRes001, ttFlyTip)
		return
	end

	--print("RetrieveResourceOfUndo, moneyType="..moneyType..", moneyNum="..moneyNum)
	if not CheckConsumeMoney( sysarg, moneyType, moneyNum, 1, false ) then
		return
	end

	if not DoConsumeMoney( sysarg, moneyType, moneyNum, rate, GameLog.clResRetrieveFee, Lang.ScriptTips.RetrieveLog01, false  ) then
		return
	end

	for idx, retrieveCfg in ipairs( ResourceRetrieveCfg ) do
		if Actor.checkActorLevel( sysarg, retrieveCfg.levelLimit[2], retrieveCfg.levelLimit[1] ) then 		--等级要求
			local opType 	= retrieveCfg.dailyOpType
			local opParam 	= retrieveCfg.dailyOpParam
			local undoTimes, getTimes, lastTimes = GetTimesOfResRetrieve( sysarg, opType, opParam, retrieveCfg.maxRetrieveTimes )
			--print("RetrieveResourceOfUndo, idx="..idx..", opType="..opType..", lastTimes="..lastTimes)
			if lastTimes > 0 then		--lastTimes是获得资源的倍数
				SetRetrieveResTimesAll( sysarg, opType, opParam )			--设置为全部领取了
				local resElem   = GetElemByOpenServerDay( retrieveCfg.resource )
				local resAwards = GetCommonAwardsWithRate( resElem.awards, lastTimes*getRate )
				if resAwards then
					GiveCommonAward(sysarg, resAwards, GameLog.clResRetrieveAward, Lang.ScriptTips.RetrieveLog02 ) 	--获得
				end
			end
		end
	end
	SendResRetrieveData( sysarg )
end

--[[
获取找回第N天的资源，优先找回最早天数的资源
]]
function GetRetrieveResDay( sysarg, opType, opParam )
	--抵扣根据创建角色的天数
	local creatDays = Actor.getDaysSinceCreateRole( sysarg ) 		--角色创建天数(1就是角色创建当天)
	local maxDays 	= creatDays - 1
	if maxDays < 0 then
		maxDays = 0
	elseif maxDays > 3 then
		maxDays = 3
	end

	for daysBefore=maxDays, 1, -1 do
		local undoTimes = GetUndoTimesBeforeByDayAndType( sysarg, daysBefore, opType, opParam ) 		--第N天前未完成的次数
		local newOpType = GetNewOpTypeByTypeAndParam( opType, opParam )
		local getTimes 	= GetRetrieveResourceTimes( sysarg, daysBefore, newOpType )						--第N天前已经领取的次数
		--print("GetRetrieveResDay, daysBefore="..daysBefore..", undoTimes="..undoTimes..", getTimes="..getTimes)
		if getTimes < undoTimes then		--未进行的次数尚未找回完毕
			return daysBefore
		end
	end
	return 0		--都已经领取了
end

function AddRetrieveResTimes( sysarg, daysBefore, newOpType, addTimes )
	--print("AddRetrieveResTimes, daysBefore="..daysBefore..", newOpType="..newOpType..", addTimes="..addTimes)
	InitRetrieveResource( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.resRetrieve[daysBefore] then
		return  false
	end

	local getTimes = sVar.resRetrieve[daysBefore][newOpType] or 0
	--print("AddRetrieveResTimes, getTimes="..getTimes..", addTimes="..addTimes)
	sVar.resRetrieve[daysBefore][newOpType] = getTimes + addTimes
	return true
end

--[[
设置为全部领取
]]
function SetRetrieveResTimesAll( sysarg, opType, opParam )
	InitRetrieveResource( sysarg )
	local maxTimes 	= GetActorDailyMaxTimes( sysarg, opType, opParam )
	local newOpType = GetNewOpTypeByTypeAndParam( opType, opParam )
	local sVar = Actor.getStaticVar(sysarg)
	sVar.resRetrieve[1][newOpType] = maxTimes 				--设置1天前全部领取
	sVar.resRetrieve[2][newOpType] = maxTimes 				--设置2天前全部领取
	sVar.resRetrieve[3][newOpType] = maxTimes 				--设置3天前全部领取
end

function SetRetrieveResTimes( sysarg, daysBefore, newOpType, newTimes )
	InitRetrieveResource( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	sVar.resRetrieve[daysBefore][newOpType] = newTimes
end


function InitRetrieveResource( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.resRetrieve then
		sVar.resRetrieve = {}
	end

	if not sVar.resRetrieve[1] then
		sVar.resRetrieve[1] = {}
	end

	if not sVar.resRetrieve[2] then
		sVar.resRetrieve[2] = {}
	end

	if not sVar.resRetrieve[3] then
		sVar.resRetrieve[3] = {}
	end
end

--[[
获取N天前已经完成某操作的次数
]]
function GetRetrieveResourceTimes( sysarg, daysBefore, newOpType )
	InitRetrieveResource( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.resRetrieve[daysBefore] then
		return 0
	end
	local getNum = sVar.resRetrieve[daysBefore][newOpType] or 0
	--if newOpType == 3 then
	--	print("GetRetrieveResourceTimes, daysBefore="..daysBefore..", newOpType="..newOpType..", getNum="..getNum)
	--end
	return getNum
end

function ClearRetrieveResourceTimes( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	sVar.resRetrieve = nil
end

function RecordRetrieveResourceTimes( sysarg, newOpType )
	RecordRetrieveResourceTimesByType( sysarg, 3, newOpType ) 				--2天前->3天前
	RecordRetrieveResourceTimesByType( sysarg, 2, newOpType ) 				--1天前->2天前
	RecordRetrieveResourceTimesByType( sysarg, 1, newOpType ) 				--当天->1天前
end

--[[
记录N天前的玩家资源找回的次数
]]
function RecordRetrieveResourceTimesByType( sysarg, daysBefore, newOpType)
	--print("RecordRetrieveResourceTimesByType, daysBefore="..daysBefore..", newOpType="..newOpType)
	if daysBefore < 1 or daysBefore > 3 then
		return
	end
	InitRetrieveResource( sysarg )
	local getTimes = 0
	if daysBefore <= 1 then
		getTimes = 0		--取当天
	else
		getTimes = GetRetrieveResourceTimes( sysarg, daysBefore-1, newOpType )
	end
	--print("RecordDailyDoneTimesBefore, doneTimes="..doneTimes)
	SetRetrieveResTimes( sysarg, daysBefore, newOpType, getTimes )  		--第N-1天前的数据迁移到第N天前的数据
end

function ShowRetrieveResourceTimes( sysarg, newOpType )
	InitRetrieveResource( sysarg )
	local getTimes1 = GetRetrieveResourceTimes( sysarg, 1, newOpType ) 		--1天前
	local getTimes2 = GetRetrieveResourceTimes( sysarg, 2, newOpType )		--2天前
	local getTimes3 = GetRetrieveResourceTimes( sysarg, 3, newOpType )		--3天前
	--print("ShowRetrieveResourceTimes, getTimes1="..getTimes1..", getTimes2="..getTimes2..", getTimes3="..getTimes3)
end

--[[
获取前3天的已经找回的全部次数
]]
function GetRetrieveResTimes( sysarg, newOpType )
	local getTimes1  = GetRetrieveResourceTimes( sysarg, 1, newOpType )
	local getTimes2  = GetRetrieveResourceTimes( sysarg, 2, newOpType )
	local getTimes3  = GetRetrieveResourceTimes( sysarg, 3, newOpType )

	--if newOpType == 3 then
	--	print("GetRetrieveResTimes, newOpType="..newOpType..", getTimes1="..getTimes1..", getTimes2="..getTimes2..", getTimes3="..getTimes3)
	--end
	return getTimes1 + getTimes2 + getTimes3
end

function ShowUndoAndRetrieveTimes( sysarg, opType, opParam  )
	local newOpType  = GetNewOpTypeByTypeAndParam( opType, opParam )
	local undoTimes1 = GetUndoTimesBeforeByDayAndType( sysarg, 1, opType, opParam )
	local getTimes1  = GetRetrieveResourceTimes( sysarg, 1, newOpType )

	local undoTimes2 = GetUndoTimesBeforeByDayAndType( sysarg, 2, opType, opParam )
	local getTimes2  = GetRetrieveResourceTimes( sysarg, 2, newOpType )

	local undoTimes3 = GetUndoTimesBeforeByDayAndType( sysarg, 2, opType, opParam )
	local getTimes3  = GetRetrieveResourceTimes( sysarg, 2, newOpType )

	--print("ShowUndoAndRetrieveTimes, undoTimes1="..undoTimes1..", getTimes1="..getTimes1.."; undoTimes2="..undoTimes2..
	--	", getTimes2="..getTimes2.."; undoTimes3="..undoTimes3..", getTimes3="..getTimes3)
end

--[[
返回某操作前3天内的
undoTimes:未进行的次数
getTimes：已找回的次数
lastTimes：可以找回的次数
]]
function GetTimesOfResRetrieve( sysarg, opType, opParam, maxTimes )
	local newOpType = GetNewOpTypeByTypeAndParam( opType, opParam )

	--if opType == 3 then
	--	print("GetTimesOfResRetrieve, opType="..opType..", newOpType="..newOpType)
	--end

	local undoTimes = GetUndoTimesBeforeByType( sysarg, opType, opParam )
	local getTimes  = GetRetrieveResTimes( sysarg, newOpType )
	local canDoTimes= undoTimes
	if undoTimes > maxTimes then
		canDoTimes = maxTimes
	end
	local lastTimes = canDoTimes - getTimes
	if lastTimes < 0 then
		lastTimes = 0
	end

	--if opType == 3 then
	--	print("GetTimesOfResRetrieve, opType="..opType..", canDoTimes="..canDoTimes..", getTimes="..getTimes..", lastTimes="..lastTimes)
	--end
	return undoTimes, getTimes, lastTimes
end

--[[
下发资源找回数据
]]
function SendResRetrieveData( sysarg )
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sSendResRetrieveData)
	if pack then
		DataPack.writeChar(pack, #ResourceRetrieveCfg)
		for idx, resCfg in ipairs( ResourceRetrieveCfg ) do
			local opType 	= resCfg.dailyOpType
			local opParam 	= resCfg.dailyOpParam
			local undoTimes, getTimes, lastTimes = GetTimesOfResRetrieve( sysarg, opType, opParam, resCfg.maxRetrieveTimes )
			DataPack.writeChar(pack, idx)
			DataPack.writeShort(pack, lastTimes)

			--if opType == 3 then
			--	print("SendResRetrieveData, idx="..idx..", opType="..opType..", undoTimes="..undoTimes..", getTimes="..getTimes..
			--		", lastTimes="..lastTimes)
			--end
		end
		DataPack.flush(pack)
	end
end

--[[
资源找回
]]
function ResRetrieve( sysarg, packet )
	local flag = DataPack.readByte(packet)
	local idx = DataPack.readByte(packet)
	RetrieveResourceOfUndo(sysarg, flag, idx )
end
